package OrientProject;
/**
*
* 自定义数组工具
*
* */
public class ArryUtil {

    public static void main(String[] args) {
        ArryUtil util = new ArryUtil();
        int[] arr = new int[]{1,4,56,78,89,99,76,57,45,-5};
        System.out.println("查找的指定数据为：" + util.getIndex(arr,56));
        util.show(arr);
        util.sort(arr);
    }

    //求数组的最大值
    public int getMax(int[] arr){
        int max = arr[0];
        for (int i = 0;i < arr.length;i++){
            if (arr[i] > max){
             max = arr[i];
            }
        }
        return max;
    }

    //求数组的最小值
    public int getMin(int[] arr){
        int min = arr[0];
        for (int i = 0;i < arr.length;i++){
            if (arr[i] < min ){
                min = arr[i];
            }
        }
        return min;
    }
    //求数组的总和
    public int getSum(int[] arr){
        int sum = 0;
        for (int i : arr){
            sum += i;
        }
        return sum;
    }
    //求数组的均值
    public int getAvg(int[] arr){

        return getSum(arr)/arr.length;
    }

    //反转数组
    public void reversal(int[] arr){
        for (int i = 0;i < arr.length/2;i++){
            int temp = arr[i];
            arr[i] = arr[arr.length - i -1];
            arr[arr.length - i -1] = temp;
        }
    }
    //复制数组
    public int[] copy(int[] arr){
        int[] arr1 = new  int[arr.length];
        for (int i = 0; i < arr1.length;i++){
            arr1[i] = arr[i];
        }
        return arr1;
    }
    //数组排序
    public void sort(int[] arr){
        for (int i = 0;i < arr.length - 1;i++){
            for (int j = 0;j < arr.length - 1 - i;j++){
                if (arr[j] > arr[j + 1]){
                 int temp = arr[j];
                 arr[j] = arr[j + 1];
                 arr[j + 1] =temp;
                }
            }
        }
    }
    //遍历数组
    public void show(int[] arr){
        for (int i : arr){
            System.out.print(i + "\t");
        }
    }
    //查找指定元素
    public int getIndex(int[] arr, int dest){

        for (int i : arr){
            if (i == dest){
                return i;
            }
        }
        return -1;
    }

}
